﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdaptiveDeNet.RAND;
using System.Threading;
using System.Runtime.InteropServices;

namespace AdaptiveDeNet.DifferentialEvolution.MultiThreaded
{
    public partial class Optimizer
    {
        public Function fEval;
        public int threadCount = Environment.ProcessorCount;



        public int ThreadCount
        {
            get { return threadCount; }
            set { threadCount = value; }
        }
        

        private ManualResetEvent mre = new ManualResetEvent(false);
        private CountdownEvent countdown = new CountdownEvent(2);
        private CountdownEvent outerCountdown = new CountdownEvent(2);

        private ManualResetEventSlim[] lockers = new ManualResetEventSlim[2];

        private ManualResetEventSlim[] mreslis = new ManualResetEventSlim[2];

        private bool condition = true;

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();

        [DllImport("kernel32.dll")]
        static extern uint GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        static extern UIntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask);

        //generator nahodnych cisel
        private Rand nRand;

        /// <summary>
        /// Contructor
        /// </summary>
        /// <param name="fEval">Fuction to be minimized</param>
        public Optimizer(Function fEval)
        {
            this.fEval = fEval;
            this.nRand = new Rand();
        }

        public Optimizer()
        {

        }

        private double FindMax(double[] Q, int[] fitnessPointers)
        {
            double ret = double.MinValue;

            for (int i = 0; i < fitnessPointers.Length; i++)
            {
                if (ret < Q[fitnessPointers[i]])
                {
                    ret = Q[fitnessPointers[i]];
                }

            }

            return ret;
        }

        private double FindMin(double[] Q, int[] fitnessPointers)
        {
            double ret = double.MaxValue;

            for (int i = 0; i < fitnessPointers.Length; i++)
            {
                if (ret > Q[fitnessPointers[i]])
                {
                    ret = Q[fitnessPointers[i]];
                }

            }



            return ret;
        }

       
    }
}
